package aTurk;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;


public class Generator implements Parameters {

	/**
	 * @param args
	 * @throws IOException 
	 */
	private static Problem getExp(ArrayList<Problem>probArr,int id,int _cost){
		int maximumX=0; 
		double maximumRevenue=0;
		int cost=_cost;
		for(int x=0;x<TOTAL_L;x++){
			ArrayList<Problem>subSet=new ArrayList<Problem>();
			for(int i=0;i<probArr.size();++i){
				if(probArr.get(i).getExpetancy()+probArr.get(i).getCost()<(cost+x))
					subSet.add(probArr.get(i));
			}
			double tempRevenue=getRevenue(subSet, cost, x);
			if(tempRevenue>maximumRevenue){
				maximumRevenue=tempRevenue;
				maximumX=x;
			}
		}
		Problem expected=new Problem(id, cost, maximumX);
		return expected;
	}
	private static Problem getOptimal(ArrayList<Problem> probArr,int id,int _cost) throws IOException{
		int maximumX=0; 
		double maximumRevenue=0;
		int cost=_cost;
		for(int x=0;x<TOTAL_L;x++){
			ArrayList<Problem>subSet=new ArrayList<Problem>();
			for(int i=0;i<probArr.size();++i){
				if(probArr.get(i).getResVal()<(cost+x))
					subSet.add(probArr.get(i));
			}
			double tempRevenue=getRevenue(subSet, cost, x);
			if(tempRevenue>maximumRevenue){
				maximumRevenue=tempRevenue;
				maximumX=x;
			}
		}
		
		/**
		 * revenue printing
		 *
		FileWriter fstream = new FileWriter("revenue.csv",true);
		BufferedWriter out = new BufferedWriter(fstream);
		out.append(String.valueOf(maximumRevenue));
		out.append("\n");
		out.close();
		**
		 * end max revenue
		 */
		
		Problem optimal=new Problem(id, cost, maximumX);
		return optimal;
	}
	private static double getPosition(ArrayList<Problem> arr,int id){
		for(int i=0;i<arr.size();++i){
			if(arr.get(i).getId()==id)
				return ((double)(i+1)/arr.size());
		}
		return 0;
	}
	private static int getPositionNumber(ArrayList<Problem> arr,int id){
		for(int i=0;i<arr.size();++i){
			if(arr.get(i).getId()==id)
				return (i+1);
		}
		return 0;
	}
	private static double getRevenue(ArrayList<Problem>arrP,int cost,int x){
		double revenue=1;
		for(int i=0;i<arrP.size();++i){
			revenue=revenue*(arrP.get(i).probabilitySmaller(x+cost));
		}
		revenue*=x;
		return revenue;
	}
	private static void shuffleStores(ArrayList<Problem> probArr,Random randGen){
		ArrayList<Integer>idxArr=new ArrayList<Integer>();
		for(int i=0;i<probArr.size();++i){
			int idx=randGen.nextInt(probArr.size())+1;
			while(idxArr.contains(idx))
				idx=randGen.nextInt(probArr.size())+1;
			idxArr.add(idx);
		}
		for(int i=0;i<probArr.size();++i){
			probArr.get(i).setId(idxArr.get(i));
		}
	}
	public static void main(String[] args) throws IOException {
	//	try
		{
		    FileWriter fstream = new FileWriter("IdOptExp.csv",true);
			BufferedWriter out = new BufferedWriter(fstream);
			Random randGen=new Random(System.currentTimeMillis());
			for(int j=0;j<PROBLEM_SIZE;j++){
				ArrayList<Problem>problemArr=new ArrayList<Problem>();
				int k=j+1;
				String filename="servers/"+k+".srv";
				PrintWriter pw=new PrintWriter(filename);
				
				/**
				 * we generate 5 random distribution stores.
				 * after we'll generate 3 stores which contains 1 bar each.
				 * 1 out of the 3 stores will be our store
				 */
				int numOfProbs=5;
				for(int i=0;i<numOfProbs;i++){
					problemArr.add(new Problem(i+1, randGen));
				}
				
				/**
				 * create two stores with one bar distribution
				 */
				int cost6=randGen.nextInt(MAX_QUERY_TIME)+1;	//1-MAX_QUERY_TIME
				int cost7=randGen.nextInt(MAX_QUERY_TIME)+1;	//1-MAX_QUERY_TIME
				int price6,price7;
				price6=randGen.nextInt(TOTAL_L);
				price7=randGen.nextInt(TOTAL_L);
				Problem p_6=new Problem(6, cost6,price6);
				Problem p_7=new Problem(7, cost7,price7);
				problemArr.add(p_6);
				problemArr.add(p_7);
				
				/**
				 * shuffle the stores
				 */
				shuffleStores(problemArr, randGen);
				Collections.sort(problemArr, new IdComperator());
				/**
				 * create the optimal problem(store)
				 */
				int cost=randGen.nextInt(MAX_QUERY_TIME)+1;	//1-MAX_QUERY_TIME
				Problem p_new=getOptimal(problemArr, numOfProbs+3, cost);

				int idx=randGen.nextInt(numOfProbs+2);
				Problem p_moved=problemArr.remove(idx);
				//changing IDs
				int tmpIdx=p_moved.getId();
				p_moved.setId(numOfProbs+3);
				p_new.setId(tmpIdx);
				problemArr.add(p_moved);
				problemArr.add(p_new);
				Collections.sort(problemArr, new IdComperator());
				
				int posNum=getPositionNumber(problemArr,p_new.getId());
				
				PrintWriter pOut=null;
				int l=1;
				for(Problem p: problemArr){
					String strDirectoy ="servers/"+k;
					new File(strDirectoy).mkdir();
					String name="servers/"+k+"/"+l+".csv";
					l++;
					pOut=new PrintWriter(name);
					pw.append(p.toString());
					pw.append("\n");
					pOut.append(p.toString());
					pOut.close();
				}
				pw.close();
				out.append(String.valueOf(posNum));
				out.append("\n");
			}
			
			out.close();
			
			
		}
		/*
		catch (Exception e) {
			System.out.println("exception"+e.);
		}*/

	}

}
